var path = require('path');
var autoprefixer = require('autoprefixer');
var precss = require('precss');
var webpack = require('webpack');
var CleanWebpackPlugin = require('clean-webpack-plugin');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var AssetsPlugin = require('assets-webpack-plugin');
var assetsPluginInstance = new AssetsPlugin({
    filename: path.join(__dirname, 'web', 'mobile', 'dist', 'manifest.json')
});
var plugins = [];
var entry = {
    app: './web/mobile/src/index.jsx'
};
if (process.env.NODE_ENV === 'production') {
    plugins = [
        new CleanWebpackPlugin(['dist'], {
            verbose: true,
            root: path.join(__dirname, 'web', 'mobile')
        }),
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false
            }
        }),
        new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.bundle.js'),
        new ExtractTextPlugin("[name].[hash].css"),
        assetsPluginInstance
    ];
    entry.vendor = ['react', 'react-router'];
} else {
    plugins = [
        new ExtractTextPlugin("style.css")
    ];
}
module.exports = {
    entry: entry,
    output: {
        publicPath: process.env.NODE_ENV !== 'production' ? 'http://192.168.0.144:8010/mobile/dist/' : '/mobile/dist/',
        filename: process.env.NODE_ENV !== 'production' ? 'mobile.bundle.js' : 'mobile.[hash:16].js',
        path: './web/mobile/dist'
    },
    postcss: function () {
        return {
            defaults: [precss, autoprefixer],
            cleaner: [autoprefixer({browsers: ["ios >= 7", "android >= 4.0"]})]
        };
    },
    module: {
        loaders: [
            {test: /\.js$/, loader: 'babel'},
            {
                test: /.jsx?$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                query: {
                    presets: ['es2015', 'react']
                }
            },
            {
                test: /\.(jpe?g|png|gif|svg)$/i,
                loaders: [
                    "file?name=img/[hash:16].[ext]",
                    "image-webpack?bypassOnDebug&optimizationLevel=9&interlaced=false"
                ]
            },
            {test: /\.css$/, loader: ExtractTextPlugin.extract('style', 'css!postcss')},
            {test: /\.scss$/, loader: ExtractTextPlugin.extract('style', 'css!postcss!sass')},
        ]
    },
    resolve: {
        extensions: ['', '.js', '.jsx']
    },
    plugins: plugins
};